package com.umeox.moto.watch.fitness.base

import android.content.Context
import com.bumptech.glide.Glide
import com.bumptech.glide.GlideBuilder
import com.bumptech.glide.Registry
import com.bumptech.glide.annotation.GlideModule
import com.bumptech.glide.integration.okhttp3.OkHttpUrlLoader
import com.bumptech.glide.load.engine.cache.DiskLruCacheFactory
import com.bumptech.glide.load.model.GlideUrl
import com.bumptech.glide.module.AppGlideModule
import com.umeox.moto.common.utils.FileUtils
import com.umeox.moto.watch.fitness.base.UnsafeOkHttpClient.getUnsafeOkHttpClient
import java.io.InputStream


@GlideModule
class GlideModule : AppGlideModule() {
    override fun applyOptions(context: Context, builder: GlideBuilder) {

        val diskCacheSizeBytes = 1024 * 1024 * 280L
        builder.setDiskCache(
            DiskLruCacheFactory(FileUtils.getAppGlideCache(), diskCacheSizeBytes)
        )

    }

    override fun registerComponents(context: Context, glide: Glide, registry: Registry) {
        val client = getUnsafeOkHttpClient()
        client?.let {
            registry.replace(GlideUrl::class.java, InputStream::class.java, OkHttpUrlLoader.Factory(client))
        }
    }

}


